﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;

using SwAddinHelpers.Contracts;
using SwAddinHelpers;
using SwAddinHelpers.Base;
using SwAddinHelpers.TaskPane;
using SwAddinHelpers.Attributes;
using SwAddinHelpers.Sinks;

using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace ExternalControls
{
    [ComVisible(true)]
    [ProgId(PROGID)]
    [Export(typeof(IExternalUserControl))]
    [HostedAsTaskpane]
    public partial class TaskPaneControl1 : TaskPaneControlBase, IExternalUserControl
    {
        public const string PROGID = "SwExternalControls.ExternalTaskPaneControl1";

        public dynamic ExternalUserControl
        {
            get { return this; }
        }

        public TaskPaneControl1()
        {
            InitializeComponent();
        }

        protected override void ThrowOnOwnerHostSetter()
        {
            propertyGrid1.SelectedObject = _OwnerHost.OwnerAddin.SolidWorksApplication.GetType();
        }

        //[SinkMethod(enumEventSink.Document_FileSaveNotify)]
        private void Document_FileSaveNotify(object sender, EventArgs e)
        {
            MessageBox.Show("From TaskPaneControl1: Document_FileSaveNotify fired");
        }

        //[SinkMethod(enumEventSink.Document_DestroyNotify)]
        private void Document_DestroyNotify(object sender, EventArgs e)
        {
            MessageBox.Show("From TaskPaneControl1: Document_DestroyNotify fired");
        }

        //[SinkMethod(enumEventSink.SW_ActiveDocChangeNotify)]
        private void ActiveDocChangedNotification(object sender, EventArgs e)
        {
            MessageBox.Show("From TaskPaneControl1: ActiveDocChangeNotify fired");
            //_OwnerHost.OwnerAddin.PropertyManagerHostCollection[0].PropertyManagerView.Show();
        }

        //[SinkMethod(enumEventSink.SW_ActiveModelDocChangeNotify)]
        private void ActiveModelDocChangedNotification(object sender, EventArgs e)
        {
            MessageBox.Show("From TaskPaneControl1: ActiveModelDocChangeNotify fired");
            //_OwnerHost.OwnerAddin.PropertyManagerHostCollection[0].PropertyManagerView.Show();
        }

        [InvokableMethod("ShowTaskPane")]
        private void ShowPane()
        {
            ((TaskPaneHost)_OwnerHost).TaskpaneView.ShowView();
        }

        [InvokableMethod("method2")]
        private void method2(int a, string b, object c)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange((string[])_OwnerHost.OwnerAddin.RetrieveUIHostByProgId("SwExternalControls.ExternalPropertyPageControlTemplate1").GetInvokableMethodNames().ToArray());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = _OwnerHost.OwnerAddin.RetrieveUIHostByProgId("SwExternalControls.ExternalPropertyPageControlTemplate1").InvokeMethod(comboBox1.Text).ToString();

            //textBox1.Text = _OwnerHost.OwnerAddin.RetrieveUIHostByControlType<PropertyManager.PropertyManagerHost>(enumHostedControl.DEFAULT_PMP).InvokeMethod("TestMethod2", new object[] { 123, "hello world"}).ToString();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            // close and open,  if you just keep opening then you're not gonna get anywhere

            // old method
            /*((SwAddinHelpers.FeatureView.FeatureViewHost)_OwnerHost.OwnerAddin.RetrieveUIHostByProgId("SwExternalControls.ExternalFeatureViewTemplateControl1")).ActivatePmpHolder();
            ((SwAddinHelpers.PropertyManager.PropertyManagerHost)_OwnerHost.OwnerAddin.RetrieveUIHostByProgId("SwExternalControls.ExternalPropertyPageControlTemplate1")).PropertyManagerView.Close(false);
            ((SwAddinHelpers.PropertyManager.PropertyManagerHost)_OwnerHost.OwnerAddin.RetrieveUIHostByProgId("SwExternalControls.ExternalPropertyPageControlTemplate1")).PropertyManagerView.Show2(0);*/

            // new method
            ((SwAddinHelpers.PropertyManager.PropertyManagerHost)_OwnerHost.OwnerAddin.RetrieveUIHostByProgId("SwExternalControls.ExternalPropertyPageControlTemplate1")).Show();
        }
    }
}


